package cn.supermarket.controller;

import cn.supermarket.pojo.dto.ProductInsertDTO;
import cn.supermarket.pojo.dto.ProductUpdateDTO;
import cn.supermarket.pojo.entity.Product;
import cn.supermarket.service.iservice.IProductService;
import cn.supermarket.web.JsonPage;
import cn.supermarket.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/product")
@Api(tags = "商品管理")
public class ProductController extends BaseController {

    private static final String PAGE_NUM = "1";
    private static final String PAGE_SIZE = "2";

    @Autowired
    private IProductService service;


    @PostMapping("/insert")
    @ApiOperation("添加商品")
    public JsonResult<String> insert(ProductInsertDTO productInsertDTO){
        service.addNew(productInsertDTO);
        return JsonResult.ok("商品添加成功: "+productInsertDTO);
    }

    @PostMapping("/delete")
    @ApiOperation("删除商品")
    public JsonResult delete(Long[] ids){
        service.deleteProduct(ids);
        return JsonResult.ok("删除成功");
    }


    @PostMapping("update/product")
    @ApiOperation("修改商品信息")
    public JsonResult<Void> updateProduct(Long id, ProductUpdateDTO productUpdateDTO){

        service.updateProductById(id,productUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/select/{id}")
    @ApiOperation("根据id查询商品")
    public JsonResult<Product> selectById(@PathVariable("id")Long id){
        Product product = service.selectById(id);
        return JsonResult.ok(product);
    }
    @GetMapping("/select/productId")
    @ApiOperation("根据商品id查询商品")
    public JsonResult<Product> selectByProductId(Integer productId){
        Product product = service.selectByProductId(productId);
        return JsonResult.ok(product);
    }

    @GetMapping("/select/title")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @ApiOperation("根据商品名查询商品")
    public JsonResult<JsonPage<Product>> selectByTitle(
            @RequestParam(required = false, defaultValue = PAGE_NUM) Integer page,
            @RequestParam(required = false, defaultValue = PAGE_SIZE) Integer pageSize,
            String title){
        JsonPage<Product> jsonPage = service.selectByTitle(page,pageSize,title);
        return JsonResult.ok(jsonPage);
    }
}
